package com.apiit.webserviceclient.activities;

import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LibraryReservations extends Activity implements OnClickListener {

	Button res;
	Button search;
	TextView message;
	EditText bookNo;
	
	// /html[1]/body[1]/p[1]
	// http://webspace1.apiit.lk/gims/library/ReservationMain.asp

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_library_reservations);
		final Intent i = new Intent(this, DisplayReservations.class);

		res = (Button) findViewById(R.id.btnMakeRes);
		search = (Button) findViewById(R.id.btnOPACSearch);
		message = (TextView) findViewById(R.id.TxtLibRes);
		bookNo = (EditText) findViewById(R.id.editTxtLibRes);
		// Message- /html[1]/body[1]/p[1]
		res.setOnClickListener(this);
		search.setOnClickListener(this);
		
		Intent intename = getIntent();
		String value = intename.getStringExtra("Resmessage");
		message.setText(value);




	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.library_reservations, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == search) {
			Intent i = new Intent(this, SearchTab.class);
			startActivity(i);
		}
		if (v == res) {
			Intent i = new Intent(this, DisplayReservations.class);
			
			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/gims/library/ReservationMain.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("message", "/html[1]/body[1]/p[1]---");
			request.setXpathMap(xpathMap);

			// book_no=JA-01-81&submit=Make+Reservation&campus=&action=NewReservation
			Map<String, String> parametersMap = new HashMap<String, String>();
			parametersMap.put("book_no", bookNo.getText().toString());
			parametersMap.put("submit", "Make+Reservation");
			// parametersMap.put("SearchType", TypeState.toString());
			parametersMap.put("action", "NewReservation");
			request.setParametersMap(parametersMap);

			request.setType("POST");
			BPEL_Response respones = null;
			try {
				respones = bpelProcess.doRequest(request);
				i.putExtra("message", respones.getResultsMap().get("message"));
				startActivity(i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			startActivity(i);
		}
	}

}
